package com.rtu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import com.rtu.dao.IProjectFieldDao;
import com.rtu.dao.IProjectPassGatewayDao;
import com.rtu.dao.IWPProjectDao;
import com.rtu.domain.ProjectField;
import com.rtu.domain.ProjectPassGateway;
import com.rtu.domain.WPProject;
import com.rtu.dto.QueryProjectDto;
import com.rtu.dto.SubmitUpdateProject;
import com.rtu.dto.UpdateProjectDto;
import com.rtu.service.IProjectService;

@Service
public class ProjectServiceImpl implements IProjectService{

	@Resource
	private IWPProjectDao projectDao;
	
	@Resource
	private IProjectPassGatewayDao projectPassGatewayDao;
	  
	@Resource
	private IProjectFieldDao projectFieldDao;
	
	@Override
	public Map query(QueryProjectDto dto) {
        Map map = new HashedMap();
        List<WPProject> list = new ArrayList<WPProject>();
        Integer count = projectDao.count();
        if (count == 0) {
            map.put("totalPage",0);
            map.put("totalCount",0);
        } else {
            int totalPage = (count % dto.getPageRowCount() == 0) ? count / dto.getPageRowCount() : count / dto.getPageRowCount() + 1;
            dto.setStart((dto.getPage() - 1) * dto.getPageRowCount());
            list = projectDao.query(dto);
            map.put("totalPage",totalPage);
            map.put("totalCount",count);
        }
        map.put("pageRowCount", dto.getPageRowCount());
        map.put("page",dto.getPage());
        map.put("list",list);
        return map;
	}


	@Override
	public Map goUpdateProject(UpdateProjectDto dto) {
		Map map = new HashedMap();
		if(null != dto && null != dto.getProjectId()) {
			WPProject project = projectDao.queryById(dto.getProjectId());
			map.put("project", project);
			List<ProjectPassGateway> passGateway = projectPassGatewayDao.query(dto.getProjectId());
			map.put("passGateway", passGateway);
		}
		return map;
	}


	@Override
	public Boolean submitUpdateProject(SubmitUpdateProject submitUpdateProject) {
		if(null != submitUpdateProject && null != submitUpdateProject.getProject()) {
			projectDao.updateProject(submitUpdateProject.getProject());
			projectPassGatewayDao.delProjectPassGatewayByProjectId(submitUpdateProject.getProject().getId());
			if(null != submitUpdateProject.getPassGateway() && submitUpdateProject.getPassGateway().size() > 0) {
				projectPassGatewayDao.batchInsert(submitUpdateProject.getPassGateway());
			}
			projectFieldDao.delProjectFieldByProjectId(submitUpdateProject.getProject().getId());
			if ((null != submitUpdateProject.getProjectField()) && (submitUpdateProject.getProjectField().size() > 0)) {
				projectFieldDao.batchInsert(submitUpdateProject.getProjectField());
			}
		}
		return true;
	}


	@Override
	public int createProject(SubmitUpdateProject submitUpdateProject) {
		projectDao.insertProject(submitUpdateProject.getProject());
		List<ProjectPassGateway> passGateways = submitUpdateProject.getPassGateway();
		if(null != passGateways && passGateways.size() > 0) {
			for(ProjectPassGateway f : passGateways) {
				if(null != f) {
					f.setProjectId(submitUpdateProject.getProject().getId());
				}
			}
			projectPassGatewayDao.batchInsert(passGateways);
		}
		for(ProjectField f : submitUpdateProject.getProjectField()) {
			if(null != f) {
				f.setProjectId(submitUpdateProject.getProject().getId());
			}
		}
		projectFieldDao.batchInsert(submitUpdateProject.getProjectField());
		return submitUpdateProject.getProject().getId();
	}


	@Override
	public Boolean updateImage(WPProject project) {
		projectDao.updateImage(project);
		return true;
	}


	@Override
	public Map queryProjectById(int projectId) {
		Map map = new HashedMap();
		WPProject project = projectDao.queryById(projectId);
		map.put("project", project);
		List<ProjectPassGateway> passGateway = projectPassGatewayDao.query(projectId);
		map.put("passGateway", passGateway);
		return map;
	}


	@Override
	public List<ProjectPassGateway> queryProjectPassGateway(int projectId) {
		List<ProjectPassGateway> passGateway = projectPassGatewayDao.query(projectId);
		return passGateway;
	}

}
